//
//  RHSystemInfoDeviceCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/2.
//

#import "RHSystemInfoDeviceCell.h"
#import "RHSystemInfoModel.h"

@interface RHSystemInfoDeviceItemCell()
@property (nonatomic ,strong) QMUILabel *deviceIdLab;

@property (nonatomic ,strong) UIButton *deleteBtn;
@end

@implementation RHSystemInfoDeviceItemCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setDeviceId:(NSString*)deviceId isEdit:(BOOL)status;
{
    self.deviceIdLab.text = deviceId;
    self.deleteBtn.hidden = !status;
}

- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = UIColor.whiteColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    [bgView rounded:8];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(3,15, 3, 15));
        make.height.mas_equalTo(35);
    }];
    
    _deviceIdLab = [[QMUILabel alloc] init];
    [_deviceIdLab setTextAlignment:NSTextAlignmentLeft];
    _deviceIdLab.numberOfLines = 0;
    [_deviceIdLab setFont:[UIFont systemFontOfSize:14]];
    [_deviceIdLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_deviceIdLab];
    
    
    
        
    _deleteBtn = [[QMUIButton alloc]init];
    [_deleteBtn setImage:[UIImage imageNamed:@"device_delete"] forState:UIControlStateNormal];
    [bgView addSubview:_deleteBtn];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-5);
        make.centerY.mas_offset(0);
        make.width.height.offset(25);
    }];
    
    [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.deleteBlock) {
            weakSelf.deleteBlock();
        }
    }];
    
    [_deviceIdLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.right.equalTo(weakSelf.deleteBtn.mas_left).offset(-5);
    }];
}
@end



//==============================================================================================


@interface RHSystemInfoDeviceCell()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUITextField *textField;

@property (nonatomic ,strong) UIImageView *arrowImg;

@property (nonatomic ,assign) CGFloat tableHeight;
@end
@implementation RHSystemInfoDeviceCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}



-(void)setModel:(RHSystemInfoModel *)model
{
    _model = model;
    self.titleLab.text = model.title;
    self.textField.placeholder = model.placeHolder;
    self.textField.text = model.content;
    if (model.unit) {
        self.textField.text = [NSString stringWithFormat:@"%@ %@",model.content,model.unit];
    }
    [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
    }];
    self.arrowImg.hidden = YES;
    if (model.isEdit) {
        self.textField.text = nil;
        if (model.canEdit) {
            self.textField.textColor = kCOLOR_HEX(@"#303133");
            if (model.isSelection) {
                self.arrowImg.hidden = NO;
                [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_offset(-35);
                }];
            }
        }else{
            self.textField.textColor = kCOLOR_HEX(@"#8B8D92");
        }
        
    }else{
        self.textField.textColor = kCOLOR_HEX(@"#303133");
        self.textField.text = [NSString stringWithFormat:@"%ld%@",self.model.deviceList.count,@"个".rh_localized];
    }
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(self.model.deviceList.count*44);
    }];
    [self.tableView reloadData];
}


- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.layer.cornerRadius = 8;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionBottom|QMUIViewBorderPositionRight;
    bgView.layer.qmui_maskedCorners = QMUILayerMinXMaxYCorner|QMUILayerMaxXMaxYCorner;
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
    }];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    _titleLab.numberOfLines = 0;
    [_titleLab setFont:[UIFont systemFontOfSize:14]];
    [_titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.width.mas_equalTo(kScaleW(100));
      
    }];
    
     _arrowImg= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:_arrowImg];
    [_arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.centerY.equalTo(weakSelf.titleLab);
        make.right.mas_offset(-15);
    }];
 
    _textField = [[QMUITextField alloc] init];
    [_textField setTextAlignment:NSTextAlignmentRight];
    [_textField setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [_textField setFont:[UIFont systemFontOfSize:14]];
    [_textField setPlaceholder:@"Please input"];
    _textField.delegate = self;
    [bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
        make.right.mas_offset(-35);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(weakSelf.titleLab);
    }];
    
        
    QMUIButton *clickBtn = [[QMUIButton alloc]init];
    [bgView addSubview:clickBtn];
    [clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab);
        make.right.equalTo(weakSelf.arrowImg);
        make.top.height.equalTo(weakSelf.textField);
        
    }];
    
    [[clickBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        //编辑状态下才选择设备
        if(weakSelf.selectBlock&&weakSelf.model.isEdit){
            weakSelf.selectBlock();
        }
    }];
    
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHSystemInfoDeviceItemCell class] forCellReuseIdentifier:@"RHSystemInfoDeviceItemCell"];
    _tableView.scrollEnabled = NO;
    [bgView addSubview:_tableView];
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
        make.height.offset(40);
    }];
    
    
    
    
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.model.deviceList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHSystemInfoDeviceItemCell *itemCell = [tableView dequeueReusableCellWithIdentifier:@"RHSystemInfoDeviceItemCell"];
    [itemCell setDeviceId:self.model.deviceList[indexPath.row] isEdit:self.model.isEdit];
    kWeakSelf;
    itemCell.deleteBlock = ^{
        if (weakSelf.deleteBlock) {
            weakSelf.deleteBlock(indexPath.row);
        }
    };
    return itemCell;
}
    


-(void)dealloc
{
   
}
@end
